home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
listings
/
v_13_05
/
allison
/
date2.c
< prev
next >
Wrap
C/C++ Source or Header
|
1995-03-12
|
1KB
|
53 lines
LISTING 5 - Implementation for Listing 4
/* date2.c */
#include <stdio.h>
#include <stdlib.h>
#include "date2.h"
struct Date
{
int month;
int day;
int year;
};
static const char *month_text[] =
{"Bad month", "January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December"};
Date *date_create(int m, int d, int y)
{
Date *dp = malloc(sizeof(Date));
if (dp == NULL)
return NULL;
dp->month = m;
dp->day = d;
dp->year = y;
return dp;
}
char *date_format(const Date *dp, char *buf)
{
sprintf(buf,"%s %d, %d",
month_text[dp->month],dp->day,dp->year);
return buf;
}
int date_compare(const Date *dp1, const Date *dp2)
{
int result = dp1->year - dp2->year;
if (result == 0)
result = dp1->month - dp2->month;
if (result == 0)
result = dp1->day - dp2->day;
return result;
}
void date_destroy(Date *dp)
{
free(dp);
}